package club.cearnach.api.security.handler;

import club.cearnach.api.vo.LoginResultVO;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author 阮胜
 * @date 2018/5/23 14:14
 */
@Component
@Slf4j
public class EmailCodeAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException {
        PrintWriter writer = response.getWriter();
        writer.write(JSONObject.toJSONString(new LoginResultVO(false, "密钥错误")));
        writer.close();
    }
}
